package com.atgm.mall.gmmallperimall.service.impl;

import com.atgm.common.utils.GmMallUtils;
import com.atgm.mall.gmmallperimall.dao.PeriGoodsIntroImageDao;
import com.atgm.mall.gmmallperimall.dao.PeriGoodsIntroImageRelaDao;
import com.atgm.mall.gmmallperimall.entity.PeriGoodsIntroImageEntity;
import com.atgm.mall.gmmallperimall.entity.PeriGoodsIntroImageRelaEntity;
import com.atgm.mall.gmmallperimall.service.PeriGoodsIntroImageService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class PeriGoodsIntroImageServiceImpl extends ServiceImpl<PeriGoodsIntroImageDao, PeriGoodsIntroImageEntity> implements PeriGoodsIntroImageService {

    @Autowired
    private PeriGoodsIntroImageRelaDao periGoodsIntroImageRelaDao;

    @Override
    public List<PeriGoodsIntroImageEntity> queryPeriGoodsIntroImageByPeriGoodsId(Long periGoodsId) {
        QueryWrapper<PeriGoodsIntroImageRelaEntity> periGoodsIntroImageRelaEntityQueryWrapper = new QueryWrapper<>();
        periGoodsIntroImageRelaEntityQueryWrapper.select("intro_image_id");
        periGoodsIntroImageRelaEntityQueryWrapper.eq("peri_goods_id", periGoodsId);
        List<PeriGoodsIntroImageRelaEntity> periGoodsIntroImageRelaEntities = periGoodsIntroImageRelaDao.selectList(periGoodsIntroImageRelaEntityQueryWrapper);

        if (GmMallUtils.isEmpty(periGoodsIntroImageRelaEntities)) {
            return null;
        }
        List<Long> introImgIdList = periGoodsIntroImageRelaEntities.stream().map(PeriGoodsIntroImageRelaEntity::getIntroImageId).collect(Collectors.toList());

        QueryWrapper<PeriGoodsIntroImageEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", introImgIdList);
        queryWrapper.orderByAsc("sort");
        List<PeriGoodsIntroImageEntity> list = baseMapper.selectList(queryWrapper);
        return list;
    }
}
